import util from '@/libs/util.js'
import { accountLoginApi, userInfoApi, exitLoginApi, notAccountApi } from '@/api/user.js'
import { md5 } from '@/libs/md5.js'

const state = {
	token: util.storage.get('token') || '',
	loginData: {},
	userInfo: {},
	accountList: [],	// 账号列表
}

const mutations = {
	// 设置token
	SET_TOKEN(state, token) {
		state.token = token
		util.storage.set('token', token)
	},
	// 清除token
	REMOVE_TOKEN(state) {
		state.token = ''
		util.storage.remove('token')
	},
	// 设置登录信息
	SET_LOGIN_DATA(state, data) {
		state.loginData = data
	},
	// 设置登录信息
	SET_USER_INFO(state, data) {
		state.loginData.login_verify = data.user.login_verify
		state.loginData.login_phone = data.user.login_phone
		state.loginData.roster_name = data.user.roster_name
		state.loginData.company_name = data.user.company_name
		state.userInfo = data
	},
	REMOVE_INFO(state) {
		state.loginData = {}
		state.userInfo = {}
	},
	SET_ACCOUNT_LISR(state, account) {
		let list = state.accountList
		list.push(account)
		// util.storage.set('accountList', state.accountList)
	}
}

const actions = {
	// 账号登录
	async login({ commit, dispatch, state, rootState }, data) {
		const { code } = await uni.login({ provider: 'weixin' })
		const params = {
			appid: rootState.common.appid,
			code,
			name: data.name,
			pwd: md5(data.pwd),
			login_type: 'wxopen'
		}
		accountLoginApi(params).then(res=>{
			commit('SET_TOKEN', res.data.access_token)
			commit('SET_LOGIN_DATA', res.data)
			commit('SET_ACCOUNT_LISR', data)
			uni.showToast({
				title: res.retmsg
			})
			setTimeout(()=>{
				if (res.data.login_verify === 1) {
					uni.navigateBack()
				} else {
					uni.redirectTo({
						url: '/pages/bind-phone/bind-phone'
					})
				}
			}, 1200)
		}).catch(err=>{
			console.log(err);
		})
	},
	
	// 无账号登录
	async notAccount({ commit, dispatch, state, rootState }, data) {
		const { code } = await uni.login({ provider: 'weixin' })
		const info = await uni.getSystemInfo()
		const { deviceId } = info
		const params = {
			appid: rootState.common.appid,
			code,
			login_type: 'wxopen',
			identifier: deviceId
		}
		notAccountApi(params).then(res=>{
			commit('SET_TOKEN', res.data.access_token)
			commit('SET_LOGIN_DATA', res.data)
			uni.showToast({
				title: res.retmsg
			})
			setTimeout(()=>{
				if (res.data.login_verify === 1) {
					uni.navigateBack()
				} else {
					uni.redirectTo({
						url: '/pages/bind-phone/bind-phone'
					})
				}
			}, 1200)
		}).catch(err=>{
			console.log('err', err);
		})
	},
	
	
	// 获取用户信息
	getUserInfo({ commit, dispatch, state, rootState }) {
		userInfoApi().then(res=>{
			commit('SET_USER_INFO', {data: res.data, user: res.user})
		}).catch(err=>{
			console.log(err);
		})
	},
	
	// 退出登录
	layout({ commit, dispatch, state, rootState }) {
		exitLoginApi().then(res=>{
			uni.showToast({
				title: res.retmsg
			})
			commit('REMOVE_TOKEN')
			commit('REMOVE_INFO')
			uni.redirectTo({
				url: '/pages/login/login'
			})
		}).catch(err=>{
			console.log(err);
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
